<?php

namespace IMT\DashboardBundle\Controller;

use Symfony\Component\HttpFoundation\Response;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;


class CalendarController extends Controller
{
    
    public function allEventAction()
    {
    	$user = $this->container->get('security.context')->getToken()->getUser();
    	//$accounts = $this->getDoctrine()->getRepository('IMTAccountBundle:Account')->findAllByUser($user);
    	$return = array();
    	
    	//foreach($user->getAccounts() as $account){
    	$events = $this->getDoctrine()->getRepository('IMTEventBundle:BaseEvent')->findAll();
    		    	
    	foreach($events as $event){
    		$accounts = $event->getAccounts();
    		$account = $accounts[0];
    		
    		$owners = $event->getOwners();
    		
    		if($account && $account->getActive() && $user->getId() == $owners[0]->getId()){
	    		switch ($event->getType()){
	    			
	    			case 'event':
	    					    		
			    	$return[] = array(
		    			"id" => $event->getId(),
		    			"title" => '<i class="icon-calendar"></i> '.$event->getTitle(),
		    			"start" => date('r', $event->getDate()->getTimestamp()),
		    			"end" => date('r', $event->getDateFin()->getTimestamp()),
		    			"allDay" => ($event->getAllDay() == 0) ? false : true,
		    			"content" => $this->renderView('IMTEventBundle:Popover:event.html.twig', array(
		    				'start'	=> $event->getDate(),
		    				'end' => $event->getDateFin(),
		    				'owners' => $event->getOwners(),
		    				'content' => $event->getContent(),
		    				'url' => $this->generateUrl('event_homepage', array('id' => $event->getId())),
		    				'delete' => $this->generateUrl('event_delete', array('id' => $event->getId())),
		    				"allDay" => ($event->getAllDay() == 0) ? false : true,
		    			)),
		    			"account" => $account->getId(),
		    			"className" => $account->getColor(),
		    			"classType" => 'Event',
	    				"type" => 'event',
		    			"isOpen" => false,
			    		"isUpdate" => false
			    	);
			    	
			    	break;
			    	
// 	    			case 'task':
	    			
// 		    		$return[] = array(
// 	    				"id" => $event->getId(),
// 	    				"title" => '<i class="icon-edit"></i> '.$event->getTitle(),
// 	    				"start" => date('r', $event->getDate()->getTimestamp()),
// 	    				"end" => date('r', $event->getDateFin()->getTimestamp()),
// 	    				"content" => $this->renderView('IMTEventBundle:Popover:task.html.twig', array(
// 		    				'start'	=> $event->getDate(),
// 		    				'end' => $event->getDateFin(),
// 		    				'owners' => $event->getOwners(),
// 		    				'content' => $event->getContent(),
// 		    				'url' => $this->generateUrl('task_homepage', array('id' => $event->getId())),
// 		    				'delete' => $this->generateUrl('task_homepage', array('id' => $event->getId())),
// 	    					"allDay" => ($event->getAllDay() == 0) ? false : true,
// 		    			)),
// 	    				"allDay" => ($event->getAllDay() == 0) ? false : true,
// 	    				"account" => $account->getId(),
// 	    				"className" => $account->getColor(),
// 	    				"classType" => 'Task',
// 	    				"type" => 'task',
// 	    				"isOpen" => false,
// 			    		"isUpdate" => false
// 		    		);
			    	
// 			    	break;
			    	
	    			case 'call':
		    			 
		    		$return[] = array(
	    				"id" => $event->getId(),
	    				"title" => '<i class="icon-phone"></i> '.$event->getTitle(),
	    				"start" => date('r', $event->getDate()->getTimestamp()),
	    				"end" => date('r', $event->getDateFin()->getTimestamp()),
	    				"content" => $this->renderView('IMTEventBundle:Popover:call.html.twig', array(
		    				'start'	=> $event->getDate(),
		    				'end' => $event->getDateFin(),
		    				'owners' => $event->getOwners(),
		    				'content' => $event->getContent(),
		    				'url' => $this->generateUrl('call_homepage', array('id' => $event->getId())),
		    				'delete' => $this->generateUrl('event_delete', array('id' => $event->getId())),
	    					"allDay" => ($event->getAllDay() == 0) ? false : true,
		    			)),
	    				"allDay" => ($event->getAllDay() == 0) ? false : true,
	    				"account" => $account->getId(),
	    				"className" => $account->getColor(),
	    				"classType" => 'Call',
	    				"type" => 'call',
	    				"isOpen" => false,
			    		"isUpdate" => false
		    		);
			    	
			    	break;
			    	
	    			case 'meeting':
	    	
	    			$return[] = array(
	    					"id" => $event->getId(),
	    					"title" => '<i class="icon-list-2"></i> '.$event->getTitle(),
	    					"start" => date('r', $event->getDate()->getTimestamp()),
	    					"end" => date('r', $event->getDateFin()->getTimestamp()),
	    					"content" => $this->renderView('IMTEventBundle:Popover:meeting.html.twig', array(
	    							'start'	=> $event->getDate(),
	    							'end' => $event->getDateFin(),
	    							'owners' => $event->getOwners(),
	    							'content' => $event->getContent(),
	    							'url' => $this->generateUrl('meeting_homepage', array('id' => $event->getId())),
	    							'delete' => $this->generateUrl('event_delete', array('id' => $event->getId())),
	    							"allDay" => ($event->getAllDay() == 0) ? false : true,
	    					)),
	    					"allDay" => ($event->getAllDay() == 0) ? false : true,
	    					"account" => $account->getId(),
	    					"className" => $account->getColor(),
	    					"classType" => 'Meeting',
	    					"type" => 'meeting',
	    					"isOpen" => false,
	    					"isUpdate" => false
	    			);
			    	
			    	break;
	    		}
    		}
    	}
    	
    	// recuperation des participations
    	$accounts = $this->getDoctrine()->getRepository('IMTAccountBundle:Account')->findByActiveAndUser(true, $user);
    		
    	foreach($accounts as $account){
    		foreach($account->getParticipantsEvent() as $participation){
    			$event = $participation->getEvent();
    			$owners = $event->getOwners();
    			
    			if($user->getId() != $owners[0]->getId()){
	    			switch ($event->getType()){
		    			
		    			case 'event':
		    					    		
				    	$return[] = array(
			    			"id" => $event->getId(),
			    			"title" => '<i class="icon-calendar"></i> '.$event->getTitle(),
			    			"start" => date('r', $event->getDate()->getTimestamp()),
			    			"end" => date('r', $event->getDateFin()->getTimestamp()),
			    			"allDay" => ($event->getAllDay() == 0) ? false : true,
			    			"content" => $this->renderView('IMTEventBundle:Popover:event.html.twig', array(
			    				'start'	=> $event->getDate(),
			    				'end' => $event->getDateFin(),
			    				'owners' => $event->getOwners(),
			    				'content' => $event->getContent(),
			    				'url' => $this->generateUrl('event_homepage', array('id' => $event->getId())),
			    				'delete' => $this->generateUrl('event_delete', array('id' => $event->getId())),
			    				"allDay" => ($event->getAllDay() == 0) ? false : true,
			    			)),
			    			"account" => $account->getId(),
			    			"className" => $account->getColor().' striped',
			    			"classType" => 'Event',
		    				"type" => 'event',
			    			"isOpen" => false,
				    		"isUpdate" => false
				    	);
				    	
				    	break;
				    	
	// 	    			case 'task':
		    			
	// 		    		$return[] = array(
	// 	    				"id" => $event->getId(),
	// 	    				"title" => '<i class="icon-edit"></i> '.$event->getTitle(),
	// 	    				"start" => date('r', $event->getDate()->getTimestamp()),
	// 	    				"end" => date('r', $event->getDateFin()->getTimestamp()),
	// 	    				"content" => $this->renderView('IMTEventBundle:Popover:task.html.twig', array(
	// 		    				'start'	=> $event->getDate(),
	// 		    				'end' => $event->getDateFin(),
	// 		    				'owners' => $event->getOwners(),
	// 		    				'content' => $event->getContent(),
	// 		    				'url' => $this->generateUrl('task_homepage', array('id' => $event->getId())),
	// 		    				'delete' => $this->generateUrl('event_delete', array('id' => $event->getId())),
	// 	    					"allDay" => ($event->getAllDay() == 0) ? false : true,
	// 		    			)),
	// 	    				"allDay" => ($event->getAllDay() == 0) ? false : true,
	// 	    				"account" => $account->getId(),
	// 	    				"className" => $account->getColor().' striped',
	// 	    				"classType" => 'Task',
	// 	    				"type" => 'task',
	// 	    				"isOpen" => false,
	// 			    		"isUpdate" => false
	// 		    		);
				    	
	// 			    	break;
				    	
		    			case 'call':
			    			 
			    		$return[] = array(
		    				"id" => $event->getId(),
		    				"title" => '<i class="icon-phone"></i> '.$event->getTitle(),
		    				"start" => date('r', $event->getDate()->getTimestamp()),
		    				"end" => date('r', $event->getDateFin()->getTimestamp()),
		    				"content" => $this->renderView('IMTEventBundle:Popover:call.html.twig', array(
			    				'start'	=> $event->getDate(),
			    				'end' => $event->getDateFin(),
			    				'owners' => $event->getOwners(),
			    				'content' => $event->getContent(),
			    				'url' => $this->generateUrl('call_homepage', array('id' => $event->getId())),
			    				'delete' => $this->generateUrl('event_delete', array('id' => $event->getId())),
		    					"allDay" => ($event->getAllDay() == 0) ? false : true,
			    			)),
		    				"allDay" => ($event->getAllDay() == 0) ? false : true,
		    				"account" => $account->getId(),
		    				"className" => $account->getColor().' striped',
		    				"classType" => 'Call',
		    				"type" => 'call',
		    				"isOpen" => false,
				    		"isUpdate" => false
			    		);
				    	
				    	break;
				    	
		    			case 'meeting':
		    	
		    			$return[] = array(
		    					"id" => $event->getId(),
		    					"title" => '<i class="icon-list-2"></i> '.$event->getTitle(),
		    					"start" => date('r', $event->getDate()->getTimestamp()),
		    					"end" => date('r', $event->getDateFin()->getTimestamp()),
		    					"content" => $this->renderView('IMTEventBundle:Popover:meeting.html.twig', array(
		    							'start'	=> $event->getDate(),
		    							'end' => $event->getDateFin(),
		    							'owners' => $event->getOwners(),
		    							'content' => $event->getContent(),
		    							'url' => $this->generateUrl('meeting_homepage', array('id' => $event->getId())),
		    							'delete' => $this->generateUrl('event_delete', array('id' => $event->getId())),
		    							"allDay" => ($event->getAllDay() == 0) ? false : true,
		    					)),
		    					"allDay" => ($event->getAllDay() == 0) ? false : true,
		    					"account" => $account->getId(),
		    					"className" => $account->getColor().' striped',
		    					"classType" => 'Meeting',
		    					"type" => 'meeting',
		    					"isOpen" => false,
		    					"isUpdate" => false
		    			);
				    	
				    	break;
		    		}
    			}
    		}   		
    	}
    	
        $return = json_encode($return);
	   	return new Response($return,200);
    }
    
    public function updateInfoEventAction()
    {
    	$request = $this->getRequest();
    	$em = $this->getDoctrine()->getEntityManager();
    	 
    	if('POST' == $request->getMethod()){
    		$idEvent = $request->request->get('idEvent');
    		$typeEvent = $request->request->get('classType');
    		$type = $request->request->get('type');
    		
    		$event = $this->getDoctrine()->getRepository('IMTEventBundle:'.$typeEvent)->find($idEvent);
    		
    		return $this->render('IMTEventBundle:Popover:'.$type.'.html.twig', array(
    				'start'	=> $event->getDate(),
    				'end' => $event->getDateFin(),
    				'owners' => $event->getOwners(),
    				'content' => $event->getContent(),
    				'url' => $this->generateUrl($type.'_homepage', array('id' => $event->getId())),
    				'delete' => $this->generateUrl($type.'_homepage', array('id' => $event->getId())),
    				"allDay" => ($event->getAllDay() == 0) ? false : true,
	    	));
    	} else
    		return new Response('Les paramètres reçus sont incorrects',200);
    		 
    	return new Response('Une erreur inconnue est survenue');
    }
    
    public function updateEventAction($type)
    {
    	$return = array('response'=>500, 'params'=>'Une erreur inconnue est survenue');
    	$request = $this->getRequest();
    	$em = $this->getDoctrine()->getEntityManager();
    	
    	if('POST' == $request->getMethod()){
    		$idEvent = $request->request->get('idEvent');
    		$classTypeEvent = $request->request->get('classType');
    		$event = $this->getDoctrine()->getRepository('IMTEventBundle:'.$classTypeEvent)->find($idEvent);
    		
    		switch($type)
    		{
    			case 'move':
    				$dayDelta = $request->request->get('dayDelta');
    				$minuteDelta = $request->request->get('minuteDelta');
    				$allDay = (intval($request->request->get('allDay')) == 0) ? false : true;
    				
    				$newDate = date('Y-m-d H:i:s', $event->getDate()->getTimestamp() + (intval($dayDelta) * 24 * 3600) + (intval($minuteDelta) * 60));
    				$newDateFin = date('Y-m-d H:i:s', $event->getDateFin()->getTimestamp() + (intval($dayDelta) * 24 * 3600) + (intval($minuteDelta) * 60));
    				
    				$event->setDate(new \DateTime($newDate));
    				$event->setDateFin(new \DateTime($newDateFin));
    				$event->setAllDay($allDay);
    				
    				try {
	    				$em->persist($event);
	    				$em->flush();
	    				
	    				$return = array('response'=>200);
    				} catch(\Exception $e){
    					return new Response(json_encode(array('response'=>500, 'params'=>'Une erreur est survenue au moment de l\'enregistrement de l\'évenement', 'debug' => $e->getMessage())),200);
    				}
    				break;
    			case 'resize':
    				$dayDelta = $request->request->get('dayDelta');
    				$minuteDelta = $request->request->get('minuteDelta');
    				
    				$newDateFin = date('Y-m-d H:i:s', $event->getDateFin()->getTimestamp() + (intval($dayDelta) * 24 * 3600) + (intval($minuteDelta) * 60));
    				
    				$event->setDateFin(new \DateTime($newDateFin));
    				
    				try {
    					$em->persist($event);
    					$em->flush();
    					 
    					$return = array('response'=>200);
    				} catch(\Exception $e){
    					return new Response(json_encode(array('response'=>500, 'params'=>'Une erreur est survenue au moment de l\'enregistrement de l\'évenement', 'debug' => $e->getMessage())),200);
    				}
    				break;
    			default:
    				return new Response(json_encode(array('response'=>500, 'params'=>'Le paramètre type reçus est incorrect ('.$type.')')),200);
    		}
    		
    	} else
    		return new Response(json_encode(array('response'=>500, 'params'=>'Les paramètres reçus sont incorrects')),200);
    	
    	$return = json_encode($return);
    	return new Response($return,200);
    }
}
